include(['com.unknown.Constants', 'com.unknown.Config'], 
		
function(){
	
	declare('com.unknown.Editor');
	
	com.unknown.CellHighlighter = function(element){
		this.element = $(element);
		this.highlight = function(cell){			
			this.element.removeClass();
			this.element.css('left', cell.getLeft());
			this.element.css('top', cell.getTop());
			this.element.addClass( 'ftile l3' );
			this.element.show();
		};
		this.invoke = function(cell){
			if( cell.isExit ){
				room.load( cell.roomId );
			}else{
				this.highlight(cell);	
			}
		};
		this.init = function(data){
			
		};
	};
	com.unknown.CellPainter = function(editor){
		this.image = null;
		this.editor = editor;
		this.invoke = function(cell){
			if( cell.isExit ){
				
			}else{
				var id = (cell.row-2)*100 + cell.column;
				$('#' + id).attr('src', this.image);
				this.editor.changes[id] = {row:cell.row-2, col:cell.column,image:this.image};
			}
		};
		this.init = function(data){
			this.image = data.image;
		};
	};
	
	com.unknown.Editor = function(maskElement, highlightElement){
		this.cell         = null;
		this.lastActionId = 0;
		this.mask         = $(maskElement);
		this.changes = {};
		this.showMenu = function(actionId){
			if( actionId != this.lastActionId ){
				this._hideMenu();
				return;
			}
			this._openDialog('#cell-menu');
		};
		this.createExitMenu = function(){
			$('.openDialog').hide();
			this.hideMenu();
			
			this._openDialog('#create-exit-form');
		};
		
		this.hideMenu = function(){
			$('openDialog').hide();
			$('openDialog').removeClass();
		};
		this._openDialog = function(id){
			$(id).removeClass();
			$(id).css('left', this.cell.getLeft());
			$(id).css('top', this.cell.getTop());
			$(id).addClass('menu');
			$(id).addClass('openDialog');
			$(id).show();
		};	
		this.save = function(){
			
		};
		
		this.initAction = function(data){
			if( data.action == 'paint' ){
				$(canvasMask).hide();
				$('#floor a').each(function(){
					$(this).hide();
				});
				gamewindow.paused = true;
			}else if( data.action == 'edit'){
				$(canvasMask).hide();
				gamewindow.paused = true;
			}else if( data.action == 'close'){
				room.mode = 'show';
				room.load( room.id );
				$(canvasMask).show();
				$('#floor a').each(function(){
					$(this).show();
				});
				gamewindow.paused = false;
			}else if( data.action == 'save'){
				$.ajax({
					url: '../room/update/' + room.id
					,type : "POST"
					,dataType : 'json'
					,data     : {id:room.id, name:room.name, tiles:JSON.stringify(this.changes)}
					,async    :true
					,beforeSend : function(x){
						if( x && x.overrideMimeType){
							x.overrideMimeType("application/j-son;charset=UTF-8");
						}
					}
					,success  : function(msg){
						alert(msg);
					}
				});
			}	
			this.hideMenu();
		};
		
		this.attach = function(){
			$('#create-exit').click(function(){
				editor.createExitMenu();
			});
			$('.cancel').click(function(){
				$('.openDialog').hide();
			});
			
			$('#create-exit-form > form > .submit').click(function(){
				$('#create-exit-form').hide();
				var serial = $('#create-exit-form > form').serialize();
				var roomId = room.id
				var cell = editor.cell;
				serial += '&row=' + cell.row + '&col=' + cell.column;
				$.ajax({
					type: 'GET',
					url: '/mud/exit/create/' + roomId,
					data: serial,
					success: function(obj){
						
						var element = '<a class="'+ cell.getStyle() + ' ftile exit" roomId="'+ obj.id +'" href="/mud/room/edit/' + obj.id + '"><span></span></a>'
						$('body').append( element );
						
					}
				});
			});
		};
		
		$('.exit').click(function(){
			actionManager.initAction({action:'exit', isExit:true, roomId:$(this).attr('roomId')})
		});
	
		$('#room-detail-form > form > .submit').click(function(){
			var serial = $('#room-detail-form > form').serialize();
			var roomId = location.pathname.substr( location.pathname.lastIndexOf('/')+1)
			$.ajax({
				type: 'GET',
				url: '/mud/room/update/' + roomId,
				data: serial,
				success: function(obj){
					alert('Success: ');
				}
			});
		});
	
	
	};
});